package com.itfreer.form.service.metadata;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.imp.DictionaryConvertServiceImp;
import com.itfreer.form.dao.metadata.MetadataDao;
import com.itfreer.form.dictionary.DictionaryManage;
import com.itfreer.form.entity.metadata.MetadataEntity;

/**
 * 定义字典服务
 */
@Component("metadataServiceImp")
public class MetadataServiceImp extends DictionaryConvertServiceImp<MetadataEntity> implements MetadataService {

	@Autowired
	private MetadataDao metadataDao;

	/**
	 * 字典服务
	 */
	@Autowired(required = false)
	private DictionaryManage dictionaryService;

	@Override
	protected BaseDao<MetadataEntity> getDao() {
		return metadataDao;
	}

	/**
	 * 添加实体
	 * 
	 * @param entity
	 *            添加实体
	 * @return
	 */
	@CacheEvict(value = "MetadataCache", allEntries = true)
	@Override
	public MetadataEntity add(MetadataEntity entity) {
		return super.add(entity);
	}

	/**
	 * 更新实体
	 * 
	 * @param guid
	 *            唯一值
	 * @param entity
	 *            实体
	 * @return
	 */
	@CacheEvict(value = "MetadataCache", allEntries = true)
	@Override
	public MetadataEntity update(MetadataEntity entity) {
		return super.update(entity);
	}

	/**
	 * 删除实体
	 * 
	 * @param guid
	 *            唯一值
	 */
	@CacheEvict(value = "MetadataCache", allEntries = true)
	@Override
	public void delete(String guid) {
		super.delete(guid);
	}

	/**
	 * 指量删除实体
	 * 
	 * @param guid
	 *            唯一值集
	 */
	@CacheEvict(value = "MetadataCache", allEntries = true)
	@Override
	public void deletes(String guids) {
		super.deletes(guids);
	}

	@Cacheable(value = "MetadataCache", key = "#metaData")
	@Override
	public List<MetadataEntity> getEntitys(String metaData) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("mid", metaData);

		Map<String, Integer> order = new HashMap<String, Integer>();
		order.put("order", 0);

		return super.getEntitys(null, where, order, 1000, 1);
	}

	@Override
	public Map<String, Object> dictionaryEscape(String mid, Map<String, Object> value) {
		if (dictionaryService == null || value == null) {
			return value;
		}
		List<MetadataEntity> metaData = getEntitys(mid);
		if (metaData == null || metaData.size() <= 0) {
			return value;
		}

		dicConvert(metaData, value);

		return value;
	}

	private void dicConvert(List<MetadataEntity> metaData, Map<String, Object> value) {
		for (MetadataEntity m : metaData) {
			String dn = m.getDictionaryName();
			String fn = m.getName();

			if (dn == null || dn.equals("") || fn == null || fn.equals("") || !value.containsKey(fn)) {
				continue;
			}

			Object dicValue = value.get(fn);
			if (dicValue == null) {
				continue;
			}

			String dc = dictionaryService.getText(dn, dicValue.toString());
			if (dc == null || dc.trim().equals("")) {
				dc = dicValue.toString();
			}
			value.put(fn + "_DESC", dc);
		}
	}

	@Override
	public List<Map<String, Object>> dictionaryEscapeEx(String mid, List<Map<String, Object>> values) {
		if (dictionaryService == null || values == null || values.size() <= 0) {
			return values;
		}

		List<MetadataEntity> metaData = getEntitys(mid);
		if (metaData == null || metaData.size() <= 0) {
			return values;
		}

		for (Map<String, Object> value : values) {
			dicConvert(metaData, value);
		}

		return values;
	}
}